$(function() {
	$("#result").on("hidden.bs.modal", function() {
		$("#response").text("");
	});

	$(".datetimepicker").datetimepicker({
		format : "yyyy-mm-dd",
		todayBtn : true,
		autoclose : true,
		minView : 2,
		language: "zh-CN"
	});

	// 分页参数条件
	function queryParams() {
		var param = {
			"name" : $("#student-name").val(),
			"classes.name" : $("#studentClasses").val(),
			"classes.product.id" : $("#studentProduct").val(),
			pageNum : this.pageNumber,
			pageSize : this.pageSize
		}
		return param;
	}

	// 教师的分页查询条件
	function teacherParams() {
		return {
			name : $("input[name='teacherName']").val(),
			type : 3,
			pageNum : this.pageNumber,
			pageSize : this.pageSize
		};
	}

	// 响应消息处理
	function responseHandler(res) {
		if (res) {
			return {
				"rows" : res.rows,
				"total" : res.total
			};
		} else {
			return {
				"rows" : [],
				"total" : 0
			};
		}
	}

	function operateFormatter(value, row, index) {
		return [ '<a class="select" href="javascript:void(0)" title="选择">',
				'<i class="glyphicon glyphicon-check">选择</i>', '</a>  ' ]
				.join('');
	}

	var operateEvents = {
		'click .select' : function(e, value, row, index) {
			$("input[name='student.id']").val(row.id);
			$("#studentName").val(row.name);
			$("#studentSelect").modal("hide");
		}
	};

	function operateFormatterTea(value, row, index) {
		return [ '<a class="selectTea" href="javascript:void(0)" title="选择">',
				'<i class="glyphicon glyphicon-check">选择</i>', '</a>  ' ]
				.join('');
	}

	var operateEventsTea = {
		'click .selectTea' : function(e, value, row, index) {
			$("input[name='teacher.id']").val(row.id);
			$("#teacherName").val(row.name);
			$("#teacherSelect").modal("hide");
		}
	};

	$("#studentForm").bootstrapTable({
		method : "get",
		dataType : "json",
		contentType : "application/json",
		cache : false,
		striped : true, // 是否显示行间隔色
		sidePagination : "server", // 服务端分页
		pagination : true,
		url : "student/lists",
		queryParams : queryParams,
		pageNumber : 1, // 初始化加载第一页，默认第一页
		pageSize : 10, // 每页的记录行数（*）
		pageList : [ 10, 25, 50, 100 ], // 可供选择的每页的行数（*）
		showExport : false,
		responseHandler : responseHandler,
		columns : [ {
			field : "id",
			title : "编号",
			align : "center",
			valign : "middle"
		}, {
			field : "name",
			title : "姓名",
			align : "center",
			valign : "middle"
		}, {
			field : "tel",
			title : "手机号码",
			align : "center",
			valign : "middle"
		}, {
			field : "classes.name",
			title : "班级",
			align : "center",
			valign : "middle"
		}, {
			field : "jobIntention",
			title : "工作意向",
			align : "center",
			valign : "middle"
		}, {
			title : "操作",
			align : "center",
			valign : "middle",
			events : operateEvents,
			formatter : operateFormatter
		} ]

	});

	$("#teacherForm").bootstrapTable({
		method : "get",
		dataType : "json",
		contentType : "application/json",
		cache : false,
		striped : true, // 是否显示行间隔色
		sidePagination : "server", // 服务端分页
		pagination : true,
		url : "teacher/lists",
		queryParams : teacherParams,
		pageNumber : 1, // 初始化加载第一页，默认第一页
		pageSize : 10, // 每页的记录行数（*）
		pageList : [ 10, 25, 50, 100 ], // 可供选择的每页的行数（*）
		showExport : false,
		responseHandler : responseHandler,
		columns : [ {
			field : "id",
			title : "教师编号",
			align : "center",
			valign : "middle"
		}, {
			field : "name",
			title : "姓名",
			align : "center",
			valign : "middle"
		}, {
			title : "操作",
			align : "center",
			valign : "middle",
			events : operateEventsTea,
			formatter : operateFormatterTea
		} ]

	});

	$(".selectTeacher").click(function() {
		$("#teacherForm").bootstrapTable("refresh");
	});

	$("button.selectStudent").click(function() {
		$("#studentForm").bootstrapTable("refresh");
	});

	$("#employmentForm").validate({
		errorElement : "label",
		errorClass : "text-danger",
		rules : {
			"student.name" : {
				required : true
			},
			"teacher.name" : {
				required : true
			},
			referNum : {
				required : true
			},
			companyName : {
				required : true
			},
			job : {
				required : true
			},
			employmentCity : {
				required : true
			},
			"employmentType.value" : {
				required : true
			},
			salary : {
				required : true
			}

		}
	});

	$("input[type='file']").change(
			function() {
				var formData = new FormData();
				formData.append("file", $(this)[0].files[0]);
				$.ajax({
					type : "POST",
					url : "upload",
					processData : false,
					contentType : false,
					enctype : 'multipart/form-data',
					cache : false,
					data : formData,
					dataType : "json",
					success : function(data) {
						$("#response").text("上传成功");
						$("#result").modal("show");
						if (data.err == "0") {
							$("#imageName").val(
									$("#imagePath").val().substring(
											$("#imagePath").val().lastIndexOf(
													'\\') + 1));
							$("input[name='imagePath']").val(
									"static/upload/" + data.msg);
							if ($("img[name='image']") != undefined) {
								$("img[name='image']").attr("src",
										"static/upload/" + data.msg);
							}
						}
					}
				});
			});

});

function clearDate(obj) {
	$(obj).closest("span").prev("input").val("");
}

function getEmployment() {
	var $companyName = $("input[name='companyName']");
	var $job = $("input[name='job']");
	var $employmentCity = $("input[name='employmentCity']");
	var $referNum = $("input[name='referNum']");
	var $salary = $("input[name='salary']");
	if ($("input[name='employmentType.value']:checked").attr("typeName") == "推荐") {
		$companyName.val() == "未知" ? $companyName.val("") : null;
		$job.val() == "未知" ? $job.val("") : null;
		$employmentCity.val() == "未知" ? $employmentCity.val("") : null;
		$referNum.val() == "0" ? $referNum.val("1") : null;
		$salary.val() == "0" ? $salary.val("") : null;
	} else {
		$companyName.val().length==0 ? $companyName.val("未知") : null;
		$job.val().length==0 ? $job.val("未知") : null;
		$employmentCity.val().length==0 ? $employmentCity.val("未知") : null;
		$referNum.val("0");
		$salary.val().length==0 ? $salary.val("0") : null;
	}
}
